<?php

require_once 'Google/Client.php';
require_once 'Google/Auth/AssertionCredentials.php';
require_once 'Google/Service/Analytics.php';

const CLIENT_ID = 'INSERT_YOUR_CLIENT_ID';
const SERVICE_ACCOUNT_NAME = 'INSERT_YOUR_SERVICE_NAME(EMAIL_FROM_CONSOLE)';
const KEY_FILE = '/INSERT_PATH_TO_YOUR_KEY_FILE/YOUR_KEY_FILE_NAME';

$key = file_get_contents(KEY_FILE);

$client = new Google_Client();
$client->setApplicationName('gTest for Management API');
$client->setClientId(CLIENT_ID);
$client->setScopes(array('https://www.googleapis.com/auth/analytics.manage.users'));
$client->setAssertionCredentials(new Google_Auth_AssertionCredentials(
    SERVICE_ACCOUNT_NAME,
    $client->getScopes(),
    $key)
);

$service = new Google_Service_Analytics($client);
$properties = $service->management_accountUserLinks->listManagementAccountUserLinks('INSERT_YOUR_GA_ACCOUNT_ID');
for ($i = 0 ; $i <= sizeof($properties->getItems()); $i++) {
  print($properties->getItems()[$i]['id'] . PHP_EOL);
  print($properties->getItems()[$i]['name'] . PHP_EOL);
}
